<?php
namespace app\api\controller;
use think\Request;
use think\Db;
use think\Controller;
use think\Session;
use changpay\ChangPay;
use app\api\controller\Order;
use app\common\controller\order\Orderget;
class Timing extends Controller {
	public function cuishourenID_fenpei(){
		$Orderget=New Orderget();
		$cuishouren=$Orderget->get_cuishouren();
		if($cuishouren=='2'){
			echo '未开启自动分配';die;
		}
		$where['YyFkTime']=array('between time',[date('Y-m-d').' 00:00:00',date('Y-m-d').' 23:59:59']);
		$re=Db::table('xb_loans_applylist a')
	    ->where($where)
		->order('ApplyTime desc')->select();
		if(empty($re)){
			echo '没有需要分配的订单';die;
			
		}
		foreach ($re as $k => $v) {
			$Orderget=New Orderget();
			$cuishourenID=$Orderget->get_kefu('51');
		 	$count[]=Db::table('xb_loans_applylist')->where($where)->update(['cuishourenID'=>$cuishourenID,'cuishourenTime' => date('Y-m-d H:i:s',time())]);
			$num=count($count);
		}
		if(!empty($num)){
//		写入日志
		$cuishourenID=$Orderget->logs('系统自动分配催收客服'.$num);
		echo '自动分配成功';die;
			
		}
		
     }
	//	定时任务自动扣款
	public function daokou_caozuo_time()
    {
    	$Orderget=New Orderget();
		$daikou=$Orderget->get_daikou();
		if($daikou=='2'){
			echo '未开启自动代扣';die;
		}
		$where['YyFkTime']=array('between time',[date('Y-m-d').' 00:00:00',date('Y-m-d').' 23:59:59']);
		$where['LoanStatus']=array('in',[2,]);
		$re=Db::table('xb_loans_applylist')
		->where($where)
		->field('ID')
		->order('ApplyTime desc')->select();
		foreach ($re as $k => $v) {
			$data['type'] =input('type');//还款类型 1还款 2续期
			$data['CreateTime']=date('Y-m-d H:i:s');
		    $order =Db::table('xb_loans_applylist')->where('ID',$v['ID'])->find();//订单数据
	        if(empty($order)){
	        	$data['Msg']='无效的订单ID';
	        	Db::table('xb_daikou')->insert($data);
	        }else{
	        	$data['OrderSn'] =$order['OrderSn'];//applylist 订单号
			    $Payceshi=$Orderget->get_Payceshi();//获取支付环境
		        if($Payceshi=='1'){
		        	if($data['type']=='1'){ 
			            $data['Money']=$order['daoqiMoney'];//还款   应还金额
		        	}else if($data['type']=='2'){					 
			            $data['Money']=$order['zhanqifei'];//续期   应还金额
		        	}else{
		        		$data['Msg']='支付环境错误';
	        	        Db::table('xb_daikou')->insert($data);
		        	}
		        }else{
			       $data['Money'] = '0.01';//放款金额
		        }
	        }
	       $user =Db::table('xb_mem_info')->where('ID',$order['UserID'])->find();//用户数据
	       $ret =Db::table('xb_renzen_bank')->where('UserID',$order['UserID'])->find();//银行卡数据
	       if($ret){
	           $data['BankNo'] =$ret['BankNo'];//银行卡号
	       }else{
	       	    $data['Msg']='用户银行卡获取失败';
	        	Db::table('xb_daikou')->insert($data);
	       }    
	       $changpay= new \changpay\ChangPay;
	       $data['PartnerId'] = $Orderget->get_changpay();//获取商户号
	       $data['userId'] =$order['UserID'];//用户标志
	       $data['OrderSn'] =$order['OrderSn'];//用户标志
	       $datas = $changpay->changjie_bank($data);
	       if($datas['AcceptStatus']=='S'){
			    $data['Msg']='提交成功,等待扣款';
	        	Db::table('xb_daikou')->insert($data);
	       }else{
	       	    $data['Msg']=$datas['RetMsg'];
	        	Db::table('xb_daikou')->insert($data);
	       }
		}
    }

}
